/**
 * Created by user on 2018/11/27.
 */
import { QaListActionType } from '../actions/qaListAction'
import { Type, Data, singleClone, GUID } from '../utils/Global'
import { Storage, Keys } from '../utils/LocalStorage'
import { cloneDeep } from 'lodash'


const qa_footer_state = {
  list: []
}

const QaListState = (state = qa_footer_state, action) => {
  const cloneState = cloneDeep(state)
  switch (action.type) {
    case QaListActionType.get_remote_self_qa_text_msg:
      let listMsg = cloneState.list.find(r => r.type === Type.COMMON_TYPE.COMMON_QA_TEXT && r.data.uuid === action.payload.msg.data.uuid)
      listMsg && (listMsg.remoteMsgId = action.payload.msg.remoteMsgId)
      return Object.assign({}, state, {
        list: cloneState.list
      })
    case QaListActionType.get_transfer_question_success_msg:
      let msg = action.payload.msg
      let newQuestionMsg = Object.assign({}, singleClone(msg), {
        type: Type.COMMON_TYPE.COMMON_QA_QUESTION,
        msgId: GUID.newGUID(30),
        sender: msg.data.question.user,
        isTeacher: 0
      })
      let replaceMsgId = msg.data.msg_id
      let listIndex = cloneState.list.findIndex(r => r.remoteMsgId === replaceMsgId)
      if (listIndex >= 0) {
        cloneState.list.splice(listIndex, 1, newQuestionMsg)
      }
      return Object.assign({}, state, {
        list: cloneState.list
      })
    case QaListActionType.get_system_qa_msg:
      return Object.assign({}, state, {
        list: cloneState.list.concat([action.msg])
      })
    case QaListActionType.init_qa:
      return Object.assign({}, state, {
        list: initList(action.list)
      })
    case QaListActionType.insert_qa:
      let cloneMsg = cloneDeep(action.item)
      if (cloneMsg.type === Type.COMMON_TYPE.COMMON_QA_VOICE) {
        cloneMsg.voiceStatus = {
          playing: false,
          hasPlayed: false,
          loading: false,
          error: false
        }
      }
      const list = state.list.concat([cloneMsg])
      let _state =  Object.assign({}, state, {
        list: list
      })
      return _state
    case QaListActionType.update_voice_status:
      const { url, status, type: msgType } = action.payload
      let currentVoiceMsg
      if (msgType === 'answer') {
        currentVoiceMsg = cloneState.list.find(m => {
          return m && m.type === Type.COMMON_TYPE.COMMON_QA_ANSWER
            && m.data.answer.data.type === 1 && m.data.answer.data.file === url
        })
      } else if (msgType === 'voice') {
        currentVoiceMsg = cloneState.list.find(m => {
          return m && m.type === Type.COMMON_TYPE.COMMON_QA_VOICE && m.data.file === url
        })
      }

      currentVoiceMsg && (currentVoiceMsg.voiceStatus = status)
      return cloneState
    case QaListActionType.stop_all_voice:
      cloneState.list.map(msg => {
        if (msg.type === Type.COMMON_TYPE.COMMON_QA_VOICE
          || (msg.type === Type.COMMON_TYPE.COMMON_QA_ANSWER && msg.data.answer.data.type === 1)) {
          msg.voiceStatus.playing = false
          msg.voiceStatus.loading = false
          msg.voiceStatus.error = false
        }
      })
      return cloneState
    case QaListActionType.do_self_like_item:
      let currentLikeItem = null
      if (action.payload.msgType === 'answer') {
        currentLikeItem = cloneState.list.find(r => {
          return r.type === Type.COMMON_TYPE.COMMON_QA_ANSWER && r.data.answer.id === action.payload.id
        })
        if (currentLikeItem) {
          currentLikeItem.data.answer.like = true
          currentLikeItem.data.answer.up_num += 1
        }
      }
      if (action.payload.msgType === 'question') {
        currentLikeItem = cloneState.list.find(r => {
          return r.type === Type.COMMON_TYPE.COMMON_QA_QUESTION && r.data.question.id === action.payload.id
        })
        if (currentLikeItem) {
          currentLikeItem.data.question.like = true
          currentLikeItem.data.question.up_num += 1
        }
      }
      return cloneState
    case QaListActionType.get_qa_question_answer_up_msg:
      let currentUpMsg = null
      let upMsg = singleClone(action.payload.msg)
      if (action.payload.msgType === 'question_up') {
        currentUpMsg = cloneState.list.find(r => {
          return r.type === Type.COMMON_TYPE.COMMON_QA_QUESTION && r.data.question.id === upMsg.data.question.id
        })
        if (currentUpMsg) {
          currentUpMsg.data.question = upMsg.data.question
        }
      } else if (action.payload.msgType === 'answer_up') {
        currentUpMsg = cloneState.list.find(r => {
          return r.type === Type.COMMON_TYPE.COMMON_QA_ANSWER && r.data.answer.id === upMsg.data.answer.id
        })
        if (currentUpMsg) {
          currentUpMsg.data.answer = upMsg.data.answer
        }
      }
      return cloneState
    case QaListActionType.get_qa_question_answer_msg:
      let qaMsg = singleClone(action.payload.msg)
      if (action.payload.msgType === 'question') {
        if (action.payload.isRemoteSelf) {
          let currentQuestionMsg = cloneState.list.find(r => {
            return r.type === Type.COMMON_TYPE.COMMON_QA_QUESTION
              && r.data.uuid === action.payload.msg.data.uuid
          })
          if (currentQuestionMsg) {
            currentQuestionMsg.data.question = singleClone(action.payload.msg.data.question)
            currentQuestionMsg.data.question_id = action.payload.msg.data.question.id
          }
        } else {
          cloneState.list.push(qaMsg)
        }
      } else if (action.payload.msgType === 'answer') {
        if (qaMsg.data.answer.data.type === 1) {
          qaMsg.voiceStatus = {
            playing: false,
            hasPlayed: false,
            loading: false,
            error: false
          }
        }
        cloneState.list.push(qaMsg)
      }
      return cloneState
    default:
      return state
  }
}
const initList = (list) => {
  const hasPlayedVoiceList = Storage.read(Keys.QA_VOICE_KEY) || []
  return list.map((msg) => {
    let hasPlayed = false
    if (msg.type === Type.COMMON_TYPE.COMMON_QA_VOICE) {
      hasPlayed = hasPlayedVoiceList.indexOf(msg.data.file) >= 0 || msg.sender.id === Data.user.id
      msg.voiceStatus = {
        playing: false,
        hasPlayed: hasPlayed,
        loading: false,
        error: false
      }
    }
    if (msg.type === Type.COMMON_TYPE.COMMON_QA_ANSWER && msg.data.answer.data.type === 1) {
      hasPlayed = hasPlayedVoiceList.indexOf(msg.data.answer.data.file) >= 0 || msg.sender.id === Data.user.id
      msg.voiceStatus = {
        playing: false,
        hasPlayed: hasPlayed,
        loading: false,
        error: false
      }
    }
    return msg
  })
}
QaListState._default_state = qa_footer_state
export default QaListState
